<?
	function drizzo_config_file_check() {
		if(file_exists('drizzo/engine/config.php') || file_exists('engine/config.php') || file_exists('config.php')) {
			return true;
		} elseif(($_SERVER['PHP_SELF'] != "/drizzo/engine/install.php") && (!file_exists('drizzo/engine/config.php') && !file_exists('engine/config.php') && !file_exists('config.php'))) {
			echo "<p>It appear Drizzo has not been installed.  Either edit '/engine/config_sample.php' and rename it to 'config.php' or <a href=\"/drizzo/engine/install.php\" title=\"Run the Installer\">click here to begin installation</a> (please make sure that '/engine' is writable - CHMOD 777).</p>";
			die();
		} else {
		}
	}
	
	function drizzo_install_check() {
		global $drizzo_options;
		
		if(!file_exists('config.php')) {
			$is_installed = 0;
		} else {
			require_once('config.php');
			require_once('functions_db.php');

			drizzo_db_connect();

			$drizzo_install_check = @mysql_query("SELECT * FROM `$drizzo_options` WHERE `name`='install_timestamp'");
			if($drizzo_install_check) {
				$is_installed = @mysql_num_rows($drizzo_install_check);
			} else {
				$is_installed = 0;
			}
		}

		return $is_installed;
	}


	function drizzo_update_version_number() {
		global $drizzo_options;

		drizzo_db_connect();

		$this_version = 0.2;
		$this_version_type = "alpha";

		$version_query = mysql_query("SELECT `value` FROM `$drizzo_options` WHERE `name`='drizzo_version_number' ORDER BY `value` DESC LIMIT 1");
		$version_query_count = mysql_num_rows($version_query);

		if($version_query_count == 0) {
			$version_insert = mysql_query("INSERT INTO `$drizzo_options` SET `name`='drizzo_version_number',`value`='$this_version'");
			if($version_insert) {
				$version_check = true;
			} else {
				$version_check = false;
			}
		} elseif($drizzo_version_number != $this_version) {
			$version_update = mysql_query("UPDATE `$drizzo_options` SET `value`='$this_version' WHERE `name`='drizzo_version_number'");
			if($version_update) {
				$version_check = true;
			} else {
				$version_check = false;
			}
		} elseif($version_query_count >= 1) {
			while($row = mysql_fetch_array($version_query)) {
				$drizzo_version_number = $row['value'];
			}
			$version_check = true;
		} else {
			$version_check = false;
		}

		$version_type_query = mysql_query("SELECT `value` FROM `$drizzo_options` WHERE `name`='version_type' ORDER BY `value` DESC LIMIT 1") or die(mysql_error());
		$version_type_query_count = mysql_num_rows($version_type_query);

		if($version_type_query_count == 0) {
			$version_type_insert = mysql_query("INSERT INTO `$drizzo_options` SET `name`='version_type',`value`='$this_version_type'");
			if($version_type_insert) {
				$version_check = true;
			} else {
				$version_check = false;
			}
		} elseif($version_type != $this_version_type) {
			$version_type_update = mysql_query("UPDATE `$drizzo_options` SET `value`='$this_version_type' WHERE `name`='version_type'");
			if($version_type_update) {
				$version_check = true;
			} else {
				$version_check = false;
			}
		} elseif($version_type_query_count >= 1) {
			while($row = mysql_fetch_array($version_type_query)) {
				$version_type = $row['value'];
			}
			$version_check = true;
		} else {
			$version_check = false;
		}
		return $version_check;
	}

	function drizzo_check_for_update() {
		global $drizzo_options;
		
		$version_check = mysql_query("SELECT `value` FROM `$drizzo_options` WHERE `name`='drizzo_version_number' LIMIT 1");
		if($version_check) {
			$drizzo_version_number = mysql_fetch_array($version_check);
			$drizzo_version_number = $drizzo_version_number[0];
		} else {
			$drizzo_version_number = 0;
		}

		$xml = file_get_contents("http://drizzoblog.com/feeds/releases.xml","r");
		if($xml) {
			$xml = trim($xml);
			$version_start = strpos($xml, "<version>");
			$version_stop = strpos($xml, "</version>");
			$version_length = $version_stop - $version_start;
			$new_drizzo_version_number = strip_tags(substr($xml, $version_start, $version_length));
			$status_start = strpos($xml, "<status>");
			$status_stop = strpos($xml, "</status>");
			$status_length = $status_stop - $status_start;
			$new_drizzo_version_status = strip_tags(substr($xml, $status_start, $status_length));
			$link_start = strpos($xml, "<download>");
			$link_stop = strpos($xml, "</download>");
			$link_length = $link_stop - $link_start;
			$new_link = strip_tags(substr($xml, $link_start, $link_length));
			if($new_drizzo_version_number > $drizzo_version_number) {
				echo "<p class=\"alert\">Drizzo " . $new_drizzo_version_number . $new_drizzo_version_status . " is now available.<br /><a href=\"" . $new_link . "\">Click here to download.</a></p>";
			} else {
				return false;
			}
		} else {
			return false;
		}
	}
?>